//
//  NearbyFriendCell.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "NearbyFriendCell.h"
#import "NearbyPhotos.h"

@interface NearbyFriendCell ()
/**
 头像
 */
@property (nonatomic , strong) UIImageView *headImg;
/**
 name
 */
@property (nonatomic , strong) UILabel *nameLab;
/**
 性别
 */
@property (nonatomic , strong) UIImageView *sexImg;
/**
 年龄
 */
@property (nonatomic , strong) UILabel *ageLab;
/**
 个性签名
 */
@property (nonatomic , strong) UILabel *remarkLab;
/**
 距离
 */
@property (nonatomic , strong) UILabel *distanceLab;
/**
 照片
 */
@property (nonatomic , strong) NearbyPhotos *photoView;
/**
 加好友和进入店铺的bgView
 */
@property (nonatomic , strong) UIView *addFrindAndGotoShopBgView;
/**
 加好友
 */
@property (nonatomic , strong) UIButton *addFriendBtn;
/**
 进店铺
 */
@property (nonatomic , strong) UIButton *gotoShopBtn;
/**
 竖线
 */
@property (nonatomic , strong) UIView *sLine;
/**
 区分按钮是 加好友  和 聊一聊
 */
@property (nonatomic , assign) AddFriendOrChatType addFriendOrChatType;
/**
 店铺数量icon
 */
@property (nonatomic , strong) UIImageView *shopNumIcon;
@end
@implementation NearbyFriendCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    NearbyFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[NearbyFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.headImg];
    [self.contentView addSubview:self.distanceLab];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.sexImg];
    [self.contentView addSubview:self.ageLab];
    [self.contentView addSubview:self.remarkLab];
    [self.contentView addSubview:self.photoView];
    [self.contentView addSubview:self.addFrindAndGotoShopBgView];
    [self.addFrindAndGotoShopBgView addSubview:self.sLine];
    [self.addFrindAndGotoShopBgView addSubview:self.addFriendBtn];
    [self.addFrindAndGotoShopBgView addSubview:self.gotoShopBtn];
    [self.addFrindAndGotoShopBgView addSubview:self.shopNumIcon];
}

- (void)initLayout {
    // 布局子控件
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(42, 42));
    }];
    [self.distanceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).offset(12);
        make.top.mas_equalTo(18);
        make.height.mas_equalTo(20);
    }];
    [self.sexImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.nameLab.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(14, 14));
        make.right.mas_equalTo(self.distanceLab.mas_left).offset(-5);
    }];
    [self.ageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).offset(12);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(2);
        make.height.mas_equalTo(17);
    }];
    [self.remarkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.headImg.mas_bottom).offset(12);
        make.right.mas_equalTo(-15);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.remarkLab.mas_bottom).offset(10);
        make.height.mas_equalTo(0);
    }];
    [self.addFrindAndGotoShopBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.photoView.mas_bottom).offset(15);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(58);
        make.bottom.mas_equalTo(-15);
    }];
    [self.sLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.addFrindAndGotoShopBgView.mas_centerX);
        make.centerY.mas_equalTo(self.addFrindAndGotoShopBgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(1, 18));
    }];
    [self.addFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.addFrindAndGotoShopBgView.mas_centerX).multipliedBy(0.5);
        make.height.mas_equalTo(58);
    }];
    [self.gotoShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.addFrindAndGotoShopBgView.mas_centerX).multipliedBy(1.5);
        make.height.mas_equalTo(58);
    }];
    [self.shopNumIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.gotoShopBtn.mas_right).offset(1);
        make.centerY.mas_equalTo(self.gotoShopBtn.mas_centerY);
    }];
}
- (void)gotoShopBtnClick {
    if (self.gotoShopBlock) {
        self.gotoShopBlock();
    }
}
- (void)addFriendBtnClick {
    if (self.addOrChatBlock) {
        self.addOrChatBlock(self.addFriendOrChatType);
    }
}
#pragma mark - getter and setter
- (void)setModel:(NearbyFriends *)model {
    _model = model;
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.nameLab.text = model.name;
    self.remarkLab.text = model.personality;
    self.distanceLab.text = [NSString stringWithFormat:@"%.2fkm",model.ranges];
    self.ageLab.text = [NSString stringWithFormat:@"年龄:%@",model.age];

    if (model.imgs.count == 1) {
        self.photoView.hidden = NO;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(128 * [JTool getScreenWidthScale]);
        }];
        [self.photoView configPhotosWithImgsArr:model.imgs];
    } else if (model.imgs.count == 2 || model.imgs.count == 3) {
        self.photoView.hidden = NO;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(109 * [JTool getScreenWidthScale]);
        }];
        [self.photoView configPhotosWithImgsArr:model.imgs];
    } else {
        self.photoView.hidden = YES;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }

    switch ([model.friendIf intValue]) {
        case IsFrinedStatusYes:
            [self.addFriendBtn setTitle:@" 聊一聊" forState:UIControlStateNormal];
            [self.addFriendBtn setImage:[UIImage imageNamed:@"chat_chat"] forState:UIControlStateNormal];
            self.addFriendOrChatType = AddFriendOrChatTypeChat;
            break;
        case IsFrinedStatusNo:
            [self.addFriendBtn setTitle:@" 加好友" forState:UIControlStateNormal];
            [self.addFriendBtn setImage:[UIImage imageNamed:@"chat_nearby_addfriend"] forState:UIControlStateNormal];
            self.addFriendOrChatType = AddFriendOrChatTypeAdd;
            break;
        default:
            break;
    }
    switch ([model.sex intValue]) {
        case SexTypeMan:
            self.sexImg.image = [UIImage imageNamed:@"user_man"];
            break;
        case SexTypeWoman:
            self.sexImg.image = [UIImage imageNamed:@"user_woman"];
            break;
        default:
            break;
    }
    
    //店铺数量
    if ([model.shopCount intValue] > 1) {
        self.shopNumIcon.hidden = NO;
    } else {
        self.shopNumIcon.hidden = YES;
    }
}
- (UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        _headImg.layer.cornerRadius = 21;
        _headImg.layer.masksToBounds = YES;
    }
    return _headImg;
}
- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = [UIColor textBlackColor];
        _nameLab.font = [UIFont middleFont];
    }
    return _nameLab;
}
- (UIImageView *)sexImg {
    if (!_sexImg) {
        _sexImg = [[UIImageView alloc] init];
        _sexImg.layer.cornerRadius = 3;
        _sexImg.layer.masksToBounds = YES;
    }
    return _sexImg;
}
- (UILabel *)distanceLab {
    if (!_distanceLab) {
        _distanceLab = [[UILabel alloc] init];
        _distanceLab.textColor = [UIColor colorWithHexString:@"#B2B2B2"];
        _distanceLab.font = [UIFont smallFont];
        _distanceLab.textAlignment = NSTextAlignmentRight;
    }
    return _distanceLab;
}
- (UILabel *)ageLab {
    if (!_ageLab) {
        _ageLab = [[UILabel alloc] init];
        _ageLab.textColor = [UIColor colorWithHexString:@"#B2B2B2"];
        _ageLab.font = [UIFont smallFont];
    }
    return _ageLab;
}
- (UILabel *)remarkLab {
    if (!_remarkLab) {
        _remarkLab = [[UILabel alloc] init];
        _remarkLab.textColor = [UIColor colorWithHexString:@"#7F7F7F"];
        _remarkLab.font = [UIFont middleFont];
        _remarkLab.numberOfLines = 0;
    }
    return _remarkLab;
}
- (UIView *)addFrindAndGotoShopBgView {
    if (!_addFrindAndGotoShopBgView) {
        _addFrindAndGotoShopBgView = [[UIView alloc] init];
        _addFrindAndGotoShopBgView.backgroundColor = [UIColor colorWithHexString:@"#FAFAF9"];
    }
    return _addFrindAndGotoShopBgView;
}
- (UIButton *)addFriendBtn {
    if (!_addFriendBtn) {
        _addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFriendBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _addFriendBtn.titleLabel.font = [UIFont middleFont];
        [_addFriendBtn addTarget:self action:@selector(addFriendBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addFriendBtn;
}
- (UIView *)sLine {
    if (!_sLine) {
        _sLine = [[UIView alloc] init];
        _sLine.backgroundColor = [UIColor colorWithHexString:@"#CBCBCB"];
    }
    return _sLine;
}
- (UIButton *)gotoShopBtn {
    if (!_gotoShopBtn) {
        _gotoShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_gotoShopBtn setTitle:@" 进店铺" forState:UIControlStateNormal];
        [_gotoShopBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _gotoShopBtn.titleLabel.font = [UIFont middleFont];
        [_gotoShopBtn setImage:[UIImage imageNamed:@"chat_nearby_shop"] forState:UIControlStateNormal];
        [_gotoShopBtn addTarget:self action:@selector(gotoShopBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _gotoShopBtn;
}
- (NearbyPhotos *)photoView {
    if (!_photoView) {
        _photoView = [[NearbyPhotos alloc] init];
    }
    return _photoView;
}
- (UIImageView *)shopNumIcon {
    if (!_shopNumIcon) {
        _shopNumIcon = [[UIImageView alloc] init];
        _shopNumIcon.image = [UIImage imageNamed:@"chat_shopnumicon"];
        _shopNumIcon.hidden = YES;
    }
    return _shopNumIcon;
}
@end
